package com.warm.pump.module.sys.service.impl;

import com.warm.pump.module.sys.bean.SysUserDto;
import com.warm.pump.module.sys.bean.po.gen.SysUser;
import com.warm.pump.module.sys.dao.gen.SysUserBoDao;
import com.warm.pump.module.sys.dao.gen.SysUserDao;
import com.warm.pump.module.sys.service.ISysUserRoleService;
import com.warm.pump.module.sys.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户实现类
 *
 * @date 2016年9月18日 上午9:46:09
 */
@Service("sysUserService")
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysUserBoDao sysUserBoDao;

    @Autowired
    private ISysUserRoleService sysUserRoleService;


    /**
     * 查询用户的所有权限
     *
     * @param userId 用户ID
     */
    public List<String> queryAllPerms(Long userId) {
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("userId", userId);
        return sysUserBoDao.queryAllPerms(condMap);
    }

    /**
     * 查询用户的所有菜单ID
     */
    public List<Long> queryAllMenuId(Long userId) {
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("userId", userId);
        return sysUserBoDao.queryAllMenuId(condMap);
    }

    /**
     * 根据用户名，查询系统用户
     */
    public SysUser queryByUserName(String username) {
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("username", username);
        return sysUserDao.selectByMap(condMap);
    }

    /**
     * 根据用户ID，查询用户
     *
     * @param userId
     * @return
     */
    public SysUser queryObject(Long userId) {
        return sysUserDao.selectByPrimaryKey(userId);
    }

    /**
     * 查询用户列表
     */
    public List<SysUser> queryList(Map<String, Object> map) {
        return sysUserDao.getListByMap(map);
    }

    /**
     * 查询总数
     */
    public int queryTotal(Map<String, Object> map) {
        return sysUserDao.countTotalByMap(map);
    }

    /**
     * 保存用户
     */
    @Transactional
    public void save(SysUserDto user) {
        user.setCreateTime(new Date());
        // sha256加密
        user.setPassword(new Sha256Hash(user.getPassword()).toHex());
        sysUserDao.insertSelective(user);

        // 保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    /**
     * 修改用户
     */
    @Transactional
    public void update(SysUserDto user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(null);
        } else {
            user.setPassword(new Sha256Hash(user.getPassword()).toHex());
        }
        sysUserDao.updateByPrimaryKeySelective(user);

        // 保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    /**
     * 删除用户
     */
    @Transactional
    public void deleteBatch(Long[] userId) {
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("userId_in", userId);
        sysUserDao.deleteByMap(condMap);
    }

    /**
     * 修改密码
     *
     * @param userId      用户ID
     * @param password    原密码
     * @param newPassword 新密码
     */
    public int updatePassword(Long userId, String password, String newPassword) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("password", password);
        SysUser sysUser = new SysUser();
        sysUser.setPassword(newPassword);

        return sysUserDao.updateByMapSelective(sysUser, map) ? 1 : 0;
    }
}
